﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Mime;

namespace FeedBack {
    public class DefaultVideoDownloader : IDownloader {
        public void Download(Story story) {
            string dir = Configuration.InProgressFolder;
            string filename = CandidateFileName(story);
            string path = Path.Combine(dir, filename);
            dir.EnsureDirectory();
            WebDownload http = new WebDownload();
            http.DownloadFileCompleted += (a, b) => {
                if (Completed != null) {
                    Completed(this, new StoryDownloadedEventArgs { Filename = path, Story = story, Success=true });
                }
            };
            for (; File.Exists(path); ) {
                Thread.Sleep(10000);
            }

            http.DownloadFileAsync(story.Source, path);
        }

        public event EventHandler<StoryDownloadedEventArgs> Completed;

        public string CandidateFileName(Story story) {
            return story.CandidateFileName(story.Source);
        }


        public void PostProcess(string storyDirectory, Story story) {
            // Do nothing
        }
    }
}
